<?php
namespace wap\controllers;


use Yii;
use common\helps\api;

/**
 * Site controller
 */
class SiteController extends CommonController
{

    //加购物车
    public function actionIndex()
    {
//        钉钉跳转链接参数格式
//        data={"userId":1,"userName":"xxx","shop":[{"shopId":1,"shopName":"xx店"},{"shopId":2,"shopName":"xx店"}]}&token=xxxxx

        $param = Yii::$app->request->get();
//        $param = array(
//            'data' =>  '{"userId":1,"userName":"xxx","shop":[{"shopId":1,"shopName":"xx店"},{"shopId":2,"shopName":"xx店"}]}',
//            'token' => 'asas'
//        );

        $data = null;
        if(isset($param['data'])) $data =json_decode($param['data'],1);

        $userInfo = array();
        if (!$data || !isset($data['userId'])) {

            //exit('<h1 style="text-align: center; padding-top: 50px;">暂无权限！</h1>');

            //测试
            $userInfo = $this->getCache('USER');
            if (!$userInfo) {
                $userInfo = array(
                    'userId' => 'test',
                    'userName' => 'test',
                    'shopId' => '9999999',
                    'shop' => array(
                        array('shopId' => 9999999, 'shopName' => '汤先生的门店', 'add' => '愚园路496号'),
                    )
                );
            }
            $this->setCache('USER', $userInfo, 0);
            return $this->render('index');
        } else {
            $userInfo['Id'] = $data['userId'];
            $userInfo['userId'] = $data['userId'];
            $userInfo['userName'] = $data['userName'];
            $shopId = '';
            if (isset($data['shop']) && !empty($data['shop']) && is_array($data['shop'])) {
                foreach ($data['shop'] as $shop) {
                    if ($shop['shopId'] == 0) {
                        $shopId = '';
                        break;
                    }

                    if ($shopId) $shopId .= ',';
                    $shopId .= $shop['shopId'];
                }
            }else{
                exit('<h1 style="text-align: center; padding-top: 50px">暂无权限！</h1>');
            }

            $shopRes = api::post('comm/get-shop', array('shopId' => $shopId));
            $shopArray = array();
            if($shopRes['code'] == 200 && is_array($shopRes['data'])) {
                foreach ($shopRes['data'] as $sd) {
                    $shopArray[] = array(
                        'shopId' => $sd['ShopID'],
                        'shopName' => $sd['ShopName'],
                        'add' => $sd['Address'],
                    );
                }
            }
            $userInfo['shopId'] = $shopId;
            $userInfo['shop'] = $shopArray;

            $this->setCache('USER', $userInfo, 0);
            return $this->render('index');
        }
        return $this->render('index');

    }


    //****************************************测试接口****************************************

    //个人中心
    public function actionUser()
    {
        $user = $this->getCache('USER');
        return $this->render('user');
    }

    public function actionAddCart()
    {
        $param = array(
            'userId' => 1,
            'list' => array(
                array(
                    'materielCode' => 'x1', //物料编号
                    'amount' => 'x1',       //数量    可选
                    'editFlg' => 'x1'       //0||不传：递增 1：修改为amount 可选
                ),
                array('materielCode' => 'x2'),
                array('materielCode' => 'x3'),
            )
        );
        var_dump(api::post('materiel/add-cart', $param));
    }

    //物料查询
    public function actionMateriel()
    {
        $param = array(
            'userId' => 1,  //可选
        );
        var_dump(api::post('materiel/get-materiel', $param));
    }

    //添加订单
    public function actionAddBill()
    {
        $param = array(

            'userId' => 11,
            'shopId' => 22,
            'list' => array(
                array('materielCode' => 'NZG01','amount' => 1),
                array('materielCode' => 'LYC01','amount' => 2),
                //array('materielCode' => 'ZRD01','amount' => 3),
            )
        );
        var_dump(api::post('bill/add-bill', $param));

    }

    //收货
    public function actionReceipt()
    {
        $param = array(

            'billNo' => '2018062611494901',
            'list' => array(
                array('materielCode' => 'NZG01','amount' => 1),
                array('materielCode' => 'LYC01','amount' => 3),
            )
        );
        var_dump(api::post('bill/receipt', $param));

    }

    //订单列表
    public function actionBillList()
    {
        $param = array(
            'userId' => '11',
            'status' => 0, //0:创建 1:审核 2:已发货 3:已收货 9:作废' 不传为全部
        );
        var_dump(api::post('bill/list', $param));
    }

    //订单详情
    public function actionBillDtl()
    {
        $param = array(
            'userId' => '11',
            'billNo' => '2018062611494901'
        );
        $res = api::post('bill/detail', $param);
        var_dump($res['data']);
    }

    public function actionTest()
    {
        $temp = array();
        $count = 0;
        while ($count < 200) {

            $password = '';
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $num = '123456789';
            for ($i = 0; $i < 6; $i++) {
                if ($i < 2) $password .= $chars[mt_rand(0, strlen($chars) - 1)];
                else $password .= $num[mt_rand(0, strlen($num) - 1)];
            }

            if (isset($temp[$password])) continue;
            $temp[$password] = true;
            echo $password;
            echo "<br>";
            $count++;
        }
    }
}
